﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Writing;
using VDS.RDF.Query;
using System.IO;

namespace proLearningEnglish
{
    public interface IResource
    {
        String getID();
    }
    
    public class Word : IResource {
                
        protected String WordID, WordLabel;

        public Word() {
            WordID = "";
            WordLabel = "";
        }

        public Word(String strLabel) {
            WordID = strLabel.Replace(" ", "_");
            WordLabel = strLabel;
        }

        public String getID() {
            return WordID;
        }

        public String getLabel() {
            return WordLabel;
        }             
    }

    public class SimpleWord : Word
    {
        private WordSense Sense;
        public SimpleWord(String strWLabel, WordSense WS)
        {
            WordID = strWLabel.Replace(" ", "_");
            WordLabel = strWLabel;
            Sense = WS;
        }
        public WordSense getSense()
        {
            return Sense;
        }

        public void setSense(WordSense ws)
        {
            Sense = ws;
        }
    }

    public class WordSense : IResource
    {

        public static String[] WORDTYPELIST = { "Noun", "Verb", "Adjective", "Adverb", "Preposition" };
        public static String[] WORDTYPESHORTLIST = { "n", "v", "adj", "adv", "pre" };
        public const int NOUN = 0, VERB = 1, ADJECTIVE = 2, ADVERB = 3, PREPOSITION = 4;
        private String WordSenseID, WordSenseLabel, WordSensePicture;
        private int intWordSenseType;

        public WordSense() {

            WordSenseID = "";
            WordSenseLabel = "";
            WordSensePicture = "";
            intWordSenseType = NOUN;
        }

        public WordSense(int intType, String strLabel, String strPicture)
        {
            intWordSenseType = intType;
            WordSenseID = strLabel.Replace(" ", "_");
            WordSenseLabel = strLabel;
            WordSensePicture = strPicture;
        }

        public String getID() {
            return WordSenseID;
        }

        public String getLabel()
        {
            return WordSenseLabel;
        }

        public void setPicture(String strWordSensePicture)
        {
            WordSensePicture = strWordSensePicture;
        }

        public String getPicture()
        {
            return WordSensePicture;
        }

        public bool isNoun()
        {
            return intWordSenseType == NOUN;
        }

        public bool isVerb()
        {
            return intWordSenseType == VERB;
        }

        public bool isAdjective()
        {
            return intWordSenseType == ADJECTIVE;
        }

        public bool isAdverb()
        {
            return intWordSenseType == ADVERB;
        }

        public bool isPreposition()
        {
            return intWordSenseType == PREPOSITION;
        }

        public int getTypeInt()
        {
            return intWordSenseType;
        }

        public String getTypeString()
        {
            return WORDTYPELIST[intWordSenseType];
        }
    }

    public class Test : IResource
    {
        protected String TestID;
        protected String Name;
        protected String DateCreate;
        protected int NumberQuestion;
        protected int Time;

        public Test(String id, String name, String date, int time)
        {
            this.TestID = id;
            this.Name = name;
            this.DateCreate = date;
            this.Time = time;
            this.NumberQuestion = 0;
        }

        public Test(String id, String name, String date, int time, int number)
        {
            this.TestID = id;
            this.Name = name;
            this.DateCreate = date;
            this.Time = time;
            this.NumberQuestion = number;
        }

        public String getName()
        {
            return Name;
        }

        public String getID()
        {
            return TestID;
        }

        public int getTime()
        {
            return this.Time;
        }

        public void setNumberQuestion( int n)
        {
            this.NumberQuestion = n;
        }

        public int getNumberQuestion()
        {
            return this.NumberQuestion;
        }

        public String getDateCreate()
        {
            return this.DateCreate;
        }
    }

    public class Question : IResource
    {
        protected String QuestionID;
        protected String QuestionLabel;
        public static String[] QUESTIONTYPELIST = { "Loại 1", "Loại 2"};
        protected int QuestionType;
        protected String DateCreate;
        protected int NumberWord;

        public Question(String qID, int type, String date)
        {
            this.QuestionID = qID;
            this.QuestionType = type;
            this.DateCreate = date;
            this.NumberWord = 0;
            this.QuestionLabel = this.QuestionID.Replace("_", " ");
        }

        public String getID()
        {
            return QuestionID;
        }

        public int getTypeInt()
        {
            return QuestionType;
        }

        public String getLabel()
        {
            return QuestionLabel;
        }

        public String getDateCreate()
        {
            return DateCreate;
        }

        public int getNumberWord()
        {
            return NumberWord;
        }
    }

    public class DataBind_Question : Question
    {
        public DataBind_Question(Question q)
            : base(q.getID(), q.getTypeInt(), q.getDateCreate())
        { }
        public DataBind_Question(String id, int type, String date): base(id, type, date)
        { }

        public String colQID
        {
            get { return QuestionID; }
            set { QuestionID = value; }
        }

        public String colQLabel
        {
            get { return QuestionLabel; }
            set { QuestionLabel = value; }
        }

        public int colQType
        {
            get { return QuestionType; }
            set { QuestionType = value; }
        }

        public string colQDate
        {
            get { return DateCreate; }
            set { DateCreate = value; }
        }

        public int colQNumberWord
        {
            get { return NumberWord; }
            set { NumberWord = value; }
        }
    }

    public class DataBind_Test : Test
    {
        public DataBind_Test(String id, String name, String date, int time)
            : base(id, name, date, time)
        {
        }
        public DataBind_Test(String id, String name, String date, int time, int numberQuestion)
            : base(id, name, date, time, numberQuestion)
        {            
        }

        public DataBind_Test(Test nTest)
            : base(nTest.getID(), nTest.getName(), nTest.getDateCreate(), nTest.getTime(), nTest.getNumberQuestion())
        {
        }
        public string colID
        {
            get { return TestID; }
            set { TestID = value; }
        }
        public string colName
        {
            get { return Name; }
            set { Name = value; }
        }
        public string colDate
        {
            get { return DateCreate; }
            set { DateCreate = value; }
        }
        public int colTime
        {
            get { return Time; }
            set { Time = value; }
        }
        public int colNumberQuestion
        {
            get { return NumberQuestion; }
            set { NumberQuestion = value; }
        }
    }

    public class QuestionCompare : IEqualityComparer<Question>
    {
        public bool Equals(Question x, Question y)
        {
            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.getID() == y.getID();
        }



        public int GetHashCode(Question obj)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(obj, null)) return 0;

            //Get hash code for the ID field if it is not null.
            int hashID = obj.getID() == null ? 0 : obj.getID().GetHashCode();

            //Calculate the hash code for the product.
            return hashID;
        }
    }

    public struct structQuestioType1
    {
        Question question;
        List<GroupBox> lstGroupBox;
        List<PictureBox> lstPictureBox;
        public structQuestioType1(Question q)
        {
            this.question = q;
            this.lstGroupBox = new List<GroupBox>();
            this.lstPictureBox = new List<PictureBox>();
            this.Init();
        }
        private void Init()
        {

        }
    }

    public class DataManager 
    {

        private Graph graph;
        private IUriNode RDFSClass, RDFType, RDFProperty, RDFSLabel,
                RDFSComment, RDFSSubClassOf, RDFSDomain, RDFSRange;
        private IUriNode ClsLesson, ClsPicture, ClsWord, ClsWordSense, ClsNoun, ClsVerb,
                ClsAdjective, ClsAdverb, ClsPreposition;
        public IUriNode HasSense, IsA, SynonymWith, AntonymWith, 
                HasPicture, InLesson, HasTitle, HasExample;
        private String strFileName;
        private Uri rdfs, rdf, word;

        private IUriNode ClsTest, ClsQuestion;
        public IUriNode JoinQuestion, JoinWord, JoinWordSense, TDateCreate, QDateCreate, HasType, Time;


        public DataManager()
        {
            graph = new Graph();
            strFileName = null;
        }

        public void loadData(String fileName)
        {
            strFileName = fileName;
            graph.Clear();
            FileLoader.Load(graph, fileName);

            rdfs = graph.NamespaceMap.GetNamespaceUri("rdfs");
            rdf = graph.NamespaceMap.GetNamespaceUri("rdf");
            word = graph.NamespaceMap.GetNamespaceUri("word");

            RDFSClass = graph.CreateUriNode(new Uri(rdfs + "class"));
            RDFType = graph.CreateUriNode(new Uri(rdf + "type"));
            RDFProperty = graph.CreateUriNode(new Uri(rdf + "property"));
            RDFSLabel = graph.CreateUriNode(new Uri(rdfs + "label"));
            RDFSComment = graph.CreateUriNode(new Uri(rdfs + "comment"));
            RDFSSubClassOf = graph.CreateUriNode(new Uri(rdfs + "subClassOf"));
            RDFSDomain = graph.CreateUriNode(new Uri(rdfs + "domain"));
            RDFSRange = graph.CreateUriNode(new Uri(rdfs + "range"));

            ClsLesson = graph.CreateUriNode("word:Lesson");

            ClsPicture = graph.CreateUriNode("word:Picture");

            ClsWord = graph.CreateUriNode("word:Word");

            ClsWordSense = graph.CreateUriNode("word:WordSense");

            ClsNoun = graph.CreateUriNode("word:Noun");

            ClsVerb = graph.CreateUriNode("word:Verb");

            ClsAdjective = graph.CreateUriNode("word:Adjective");

            ClsAdverb = graph.CreateUriNode("word:Adverb");

            ClsPreposition = graph.CreateUriNode("word:Preposition");

            ClsTest = graph.CreateUriNode("word:Test");

            ClsQuestion = graph.CreateUriNode("word:Question");


            HasSense = graph.CreateUriNode("word:hasSense");

            IsA = graph.CreateUriNode("word:isA");

            SynonymWith = graph.CreateUriNode("word:synonymWith");

            AntonymWith = graph.CreateUriNode("word:antonymWith");

            HasPicture = graph.CreateUriNode("word:hasPicture");

            InLesson = graph.CreateUriNode("word:inLesson");

            HasTitle = graph.CreateUriNode("word:hasTitle");            
            
            JoinQuestion = graph.CreateUriNode("word:joinQuestion");
            
            JoinWord = graph.CreateUriNode("word:joinWord");
            
            JoinWordSense = graph.CreateUriNode("word:joinWordSense");
            
            TDateCreate = graph.CreateUriNode("word:tdateCreate");

            QDateCreate = graph.CreateUriNode("word:qdateCreate");
            
            HasType = graph.CreateUriNode("word:hasType");

            Time = graph.CreateUriNode("word:time");
        }

        //create new file data
        public void newData()
        {
            graph.Clear();
            word = new Uri("http://www.learning-english.org/#");
            graph.NamespaceMap.AddNamespace("word", word);
            graph.BaseUri = word;

            rdfs = graph.NamespaceMap.GetNamespaceUri("rdfs");
            rdf = graph.NamespaceMap.GetNamespaceUri("rdf");
            word = graph.NamespaceMap.GetNamespaceUri("word");


            RDFSClass = graph.CreateUriNode(new Uri(rdfs + "class"));
            RDFType = graph.CreateUriNode(new Uri(rdf + "type"));
            RDFProperty = graph.CreateUriNode(new Uri(rdf + "property"));
            RDFSLabel = graph.CreateUriNode(new Uri(rdfs + "label"));
            RDFSComment = graph.CreateUriNode(new Uri(rdfs + "comment"));
            RDFSSubClassOf = graph.CreateUriNode(new Uri(rdfs + "subClassOf"));
            RDFSDomain = graph.CreateUriNode(new Uri(rdfs + "domain"));
            RDFSRange = graph.CreateUriNode(new Uri(rdfs + "range"));

            ILiteralNode locNodeVal;
            ClsLesson = graph.CreateUriNode("word:Lesson");
            graph.Assert(new Triple(ClsLesson, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Bài");
            graph.Assert(new Triple(ClsLesson, RDFSLabel, locNodeVal));

            ClsWord = graph.CreateUriNode("word:Word");
            graph.Assert(new Triple(ClsWord, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Từ");
            graph.Assert(new Triple(ClsWord, RDFSLabel, locNodeVal));

            ClsWordSense = graph.CreateUriNode("word:WordSense");
            graph.Assert(new Triple(ClsWordSense, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Nghĩa của từ");
            graph.Assert(new Triple(ClsWordSense, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsWordSense, RDFSSubClassOf, ClsWord));

            ClsNoun = graph.CreateUriNode("word:Noun");
            graph.Assert(new Triple(ClsNoun, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Noun");
            graph.Assert(new Triple(ClsNoun, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsNoun, RDFSSubClassOf, ClsWordSense));

            ClsVerb = graph.CreateUriNode("word:Verb");
            graph.Assert(new Triple(ClsVerb, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Verb");
            graph.Assert(new Triple(ClsVerb, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsVerb, RDFSSubClassOf, ClsWordSense));

            ClsAdjective = graph.CreateUriNode("word:Adjective");
            graph.Assert(new Triple(ClsAdjective, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Adjective");
            graph.Assert(new Triple(ClsAdjective, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsAdjective, RDFSSubClassOf, ClsWordSense));

            ClsAdverb = graph.CreateUriNode("word:Adverb");
            graph.Assert(new Triple(ClsAdverb, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Adverb");
            graph.Assert(new Triple(ClsAdverb, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsAdverb, RDFSSubClassOf, ClsWordSense));

            ClsPreposition = graph.CreateUriNode("word:Preposition");
            graph.Assert(new Triple(ClsPreposition, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Preposition");
            graph.Assert(new Triple(ClsPreposition, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsPreposition, RDFSSubClassOf, ClsWordSense));

            ClsTest = graph.CreateUriNode("word:Test");
            graph.Assert(new Triple(ClsTest, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Đề kiểm tra");
            graph.Assert(new Triple(ClsTest, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsTest, RDFSSubClassOf, ClsWord));

            ClsQuestion = graph.CreateUriNode("word:Question");
            graph.Assert(new Triple(ClsQuestion, RDFType, RDFSClass));
            locNodeVal = graph.CreateLiteralNode("Câu hỏi");
            graph.Assert(new Triple(ClsQuestion, RDFSLabel, locNodeVal));
            graph.Assert(new Triple(ClsQuestion, RDFSSubClassOf, ClsWord));   

            HasSense = graph.CreateUriNode("word:hasSense");
            locNodeVal = graph.CreateLiteralNode("có nghĩa là");
            graph.Assert(new Triple(HasSense, RDFType, RDFProperty));
            graph.Assert(new Triple(HasSense, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(HasSense, RDFSRange, ClsWordSense));
            graph.Assert(new Triple(HasSense, RDFSLabel, locNodeVal));

            IsA = graph.CreateUriNode("word:isA");
            locNodeVal = graph.CreateLiteralNode("Là một");
            graph.Assert(new Triple(IsA, RDFType, RDFProperty));
            graph.Assert(new Triple(IsA, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(IsA, RDFSRange, ClsWordSense));
            graph.Assert(new Triple(IsA, RDFSLabel, locNodeVal));

            SynonymWith = graph.CreateUriNode("word:synonymWith");
            locNodeVal = graph.CreateLiteralNode("Đồng nghĩa với");
            graph.Assert(new Triple(SynonymWith, RDFType, RDFProperty));
            graph.Assert(new Triple(SynonymWith, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(SynonymWith, RDFSRange, ClsWordSense));
            graph.Assert(new Triple(SynonymWith, RDFSLabel, locNodeVal));

            AntonymWith = graph.CreateUriNode("word:antonymWith");
            locNodeVal = graph.CreateLiteralNode("Trái nghĩa với");
            graph.Assert(new Triple(AntonymWith, RDFType, RDFProperty));
            graph.Assert(new Triple(AntonymWith, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(AntonymWith, RDFSRange, ClsWordSense));
            graph.Assert(new Triple(AntonymWith, RDFSLabel, locNodeVal));

            HasPicture = graph.CreateUriNode("word:hasPicture");
            locNodeVal = graph.CreateLiteralNode("Ảnh miêu tả nghĩa của từ");
            graph.Assert(new Triple(HasPicture, RDFType, RDFProperty));
            graph.Assert(new Triple(HasPicture, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(HasPicture, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "string"))));
            graph.Assert(new Triple(HasPicture, RDFSLabel, locNodeVal));

            InLesson = graph.CreateUriNode("word:inLesson");
            locNodeVal = graph.CreateLiteralNode("Trong bài học");
            graph.Assert(new Triple(InLesson, RDFType, RDFProperty));
            graph.Assert(new Triple(InLesson, RDFSDomain, ClsWordSense));
            graph.Assert(new Triple(InLesson, RDFSRange, ClsLesson));
            graph.Assert(new Triple(InLesson, RDFSLabel, locNodeVal));

            HasTitle = graph.CreateUriNode("word:hasTitle");
            locNodeVal = graph.CreateLiteralNode("Tiêu đề");
            graph.Assert(new Triple(HasTitle, RDFType, RDFProperty));
            graph.Assert(new Triple(HasTitle, RDFSDomain, ClsLesson));
            graph.Assert(new Triple(HasTitle, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "string"))));
            graph.Assert(new Triple(HasTitle, RDFSLabel, locNodeVal));

            //test            

            Time = graph.CreateUriNode("word:time");
            locNodeVal = graph.CreateLiteralNode("Thời gian làm bài kiểm tra");
            graph.Assert(new Triple(Time, RDFType, RDFProperty));
            graph.Assert(new Triple(Time, RDFSDomain, ClsTest));
            graph.Assert(new Triple(Time, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "integer"))));
            graph.Assert(new Triple(Time, RDFSLabel, locNodeVal));

            TDateCreate = graph.CreateUriNode("word:tdateCreate");
            locNodeVal = graph.CreateLiteralNode("ngày tạo đề kiểm tra");
            graph.Assert(new Triple(TDateCreate, RDFType, RDFProperty));
            graph.Assert(new Triple(TDateCreate, RDFSDomain, ClsTest));
            graph.Assert(new Triple(TDateCreate, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "string"))));
            graph.Assert(new Triple(TDateCreate, RDFSLabel, locNodeVal));

            JoinQuestion = graph.CreateUriNode("word:joinQuestion");
            locNodeVal = graph.CreateLiteralNode("Câu hỏi trong đề kiểm tra");
            graph.Assert(new Triple(JoinQuestion, RDFType, RDFProperty));
            graph.Assert(new Triple(JoinQuestion, RDFSDomain, ClsTest));
            graph.Assert(new Triple(JoinQuestion, RDFSRange, ClsQuestion));
            graph.Assert(new Triple(JoinQuestion, RDFSLabel, locNodeVal));

            //Question

                     

            JoinWord = graph.CreateUriNode("word:joinWord");
            locNodeVal = graph.CreateLiteralNode("Từ tiếng Anh tham gia vào câu hỏi");
            graph.Assert(new Triple(JoinWord, RDFType, RDFProperty));
            graph.Assert(new Triple(JoinWord, RDFSDomain, ClsQuestion));
            graph.Assert(new Triple(JoinWord, RDFSRange, ClsWord));
            graph.Assert(new Triple(JoinWord, RDFSLabel, locNodeVal));

            JoinWordSense = graph.CreateUriNode("word:joinWordSense");
            locNodeVal = graph.CreateLiteralNode("Từ tiếng Anh tham gia vào câu hỏi");
            graph.Assert(new Triple(JoinWordSense, RDFType, RDFProperty));
            graph.Assert(new Triple(JoinWordSense, RDFSDomain, ClsQuestion));
            graph.Assert(new Triple(JoinWordSense, RDFSRange, ClsWord));
            graph.Assert(new Triple(JoinWordSense, RDFSLabel, locNodeVal));

            QDateCreate = graph.CreateUriNode("word:qdateCreate");
            locNodeVal = graph.CreateLiteralNode("ngày tạo câu hỏi");
            graph.Assert(new Triple(QDateCreate, RDFType, RDFProperty));
            graph.Assert(new Triple(QDateCreate, RDFSDomain, ClsQuestion));
            graph.Assert(new Triple(QDateCreate, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "string"))));
            graph.Assert(new Triple(QDateCreate, RDFSLabel, locNodeVal));

            HasType = graph.CreateUriNode("word:hasType");
            locNodeVal = graph.CreateLiteralNode("Loại câu hỏi");
            graph.Assert(new Triple(HasType, RDFType, RDFProperty));
            graph.Assert(new Triple(HasType, RDFSDomain, ClsQuestion));
            graph.Assert(new Triple(HasType, RDFSRange, graph.CreateUriNode(new Uri(graph.NamespaceMap.GetNamespaceUri("xsd") + "integer"))));
            graph.Assert(new Triple(HasType, RDFSLabel, locNodeVal));

        }
		
		public void addWord(Word objWord) {
			IUriNode node = graph.CreateUriNode("word:" + objWord.getID());
			ILiteralNode label = graph.CreateLiteralNode(objWord.getLabel());
            graph.Assert(new Triple(node, RDFType, ClsWord));
			graph.Assert(new Triple(node, RDFSLabel, label));			             
		}

        public void addTest(Test ts)
        {
            IUriNode node = graph.CreateUriNode("word:" + ts.getID());
            ILiteralNode label = graph.CreateLiteralNode(ts.getName());
            ILiteralNode time = graph.CreateLiteralNode(ts.getTime().ToString());
            ILiteralNode date = graph.CreateLiteralNode(ts.getDateCreate().ToString());
            graph.Assert(new Triple(node, RDFType, ClsTest));
            graph.Assert(new Triple(node, RDFSLabel, label));
            graph.Assert(new Triple(node, Time, time));
            graph.Assert(new Triple(node, TDateCreate, date));
        }

        public void removeTest(Test tst)
        {
            IUriNode node = graph.CreateUriNode("word:" + tst.getID());
            graph.Retract(graph.GetTriplesWithSubject(node));
            //graph.Retract(graph.GetTriplesWithObject(node));
        }

        public void removeQuestion(Question q)
        {
            IUriNode node = graph.CreateUriNode("word:" + q.getID());
            graph.Retract(graph.GetTriplesWithSubject(node));
            graph.Retract(graph.GetTriplesWithObject(node));
        }

        public void addQuestion(Question qt)
        {
            IUriNode node = graph.CreateUriNode("word:" + qt.getID());
            ILiteralNode label = graph.CreateLiteralNode(qt.getLabel());
            ILiteralNode type = graph.CreateLiteralNode(qt.getTypeInt().ToString());
            ILiteralNode date = graph.CreateLiteralNode(qt.getDateCreate());
            graph.Assert(new Triple(node, RDFType, ClsQuestion));
            graph.Assert(new Triple(node, RDFSLabel, label));
            graph.Assert(new Triple(node, HasType, type));
            graph.Assert(new Triple(node, QDateCreate, date));
        }
        		
		public void removeWord(Word objWord) {
			IUriNode node = graph.CreateUriNode("word:" + objWord.getID());
			graph.Retract(graph.GetTriplesWithSubject(node));
			graph.Retract(graph.GetTriplesWithObject(node));
		}
        
        public void addRelation(Word objWord, IUriNode objNode, WordSense objWordSense)
        {
			INode nWord, nWordSense;			
			nWord = graph.CreateUriNode("word:" + objWord.getID());
			nWordSense = graph.CreateUriNode("word:" + objWordSense.getID());	
			graph.Retract(graph.GetTriplesWithSubjectPredicate(nWord, objNode));									
			graph.Assert(new Triple(nWord, objNode, nWordSense));			
		}

        public void addRelation(WordSense objWordSense1, IUriNode objNode, WordSense objWordSense2)
        {
            INode nWordSense1, nWordSense2;
            nWordSense1 = graph.CreateUriNode("word:" + objWordSense1.getID());
            nWordSense2 = graph.CreateUriNode("word:" + objWordSense2.getID());
            graph.Retract(new Triple(nWordSense1, objNode, nWordSense2));
            graph.Assert(new Triple(nWordSense1, objNode, nWordSense2));
        }

        public void addRelation(Question objQ, SimpleWord objSW)
        {
            INode nQuestion, nWord, nSense;
            nQuestion = graph.CreateUriNode("word:" + objQ.getID());
            nWord = graph.CreateUriNode("word:" + objSW.getID());
            nSense = graph.CreateUriNode("word:" + objSW.getSense().getID());
            //graph.Retract(new Triple(nQuestion, JoinWord, nWord));
            //graph.Retract(new Triple(nQuestion, JoinWordSense, nSense));
            graph.Assert(new Triple(nQuestion, JoinWord, nWord));
            graph.Assert(new Triple(nQuestion, JoinWordSense, nSense));

        }

        public void addRelation(Question objQ, List<SimpleWord> lstSW)
        {
            INode nQuestion, nWord, nSense;
            foreach (SimpleWord objSW in lstSW)
            {
                nQuestion = graph.CreateUriNode("word:" + objQ.getID());
                nWord = graph.CreateUriNode("word:" + objSW.getID());
                nSense = graph.CreateUriNode("word:" + objSW.getSense().getID());
                //graph.Retract(new Triple(nQuestion, JoinWord, nWord));
                //graph.Retract(new Triple(nQuestion, JoinWordSense, nSense));
                graph.Assert(new Triple(nQuestion, JoinWord, nWord));
                graph.Assert(new Triple(nQuestion, JoinWordSense, nSense));
            }

        }

        public void addRelation(Question objQ, Word objW)
        {
            INode nQuestion, nWord;
            nQuestion = graph.CreateUriNode("word:" + objQ.getID());
            nWord = graph.CreateUriNode("word:" + objW.getID());
            graph.Assert(new Triple(nQuestion, JoinWord, nWord));
        }

        public void addRelation(Question objQ, WordSense objWS)
        {
            INode nQuestion, nSense;
            nQuestion = graph.CreateUriNode("word:" + objQ.getID());
            nSense = graph.CreateUriNode("word:" + objWS.getID());
            graph.Assert(new Triple(nQuestion, JoinWordSense, nSense));
        }

        public void addRelation(Question objQ, List<WordSense> lstWS)
        {
            INode nQuestion, nSense;
            nQuestion = graph.CreateUriNode("word:" + objQ.getID());
            foreach (WordSense s in lstWS)
            {
                nSense = graph.CreateUriNode("word:" + s.getID());
                graph.Assert(new Triple(nQuestion, JoinWordSense, nSense));
            }
        }

        public void addRelation(Test t, IUriNode objNode, Question q)
        {
            INode nQuestion, nTest;
            nQuestion = graph.CreateUriNode("word:" + q.getID());
            nTest = graph.CreateUriNode("word:" + t.getID());
            graph.Assert(new Triple(nTest, JoinQuestion, nQuestion));
        }

        public void addRelation(Test t, IUriNode objNode, List<Question> lstQ)
        {
            foreach (Question q in lstQ)
            {
                addRelation(t, objNode, q);
            }
        }
		
		public void removeRelation(IResource objWord, IUriNode objNode) {
			graph.Retract(graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode("word:" + objWord.getID()), objNode));
		}

        public void removeRelation(IResource objWord1, IUriNode objNode, IResource objWord2)
        {
			graph.Retract(new Triple(graph.CreateUriNode("word:" + objWord1.getID()), objNode, graph.CreateUriNode("word:" + objWord2.getID())));
		}
		
		public void addWordSense(WordSense objWordSense) {
            //System.Windows.Forms.MessageBox.Show(objWordSense.getWordSenseType().ToString());
            IUriNode node = graph.CreateUriNode("word:" + objWordSense.getID());
            ILiteralNode label = graph.CreateLiteralNode(objWordSense.getLabel());
            switch (objWordSense.getTypeInt())
            {
                case WordSense.NOUN:
                    graph.Assert(new Triple(node, RDFType, ClsNoun));
                    break;
                case WordSense.VERB:
                    graph.Assert(new Triple(node, RDFType, ClsVerb));
                    break;
                case WordSense.ADVERB:
                    graph.Assert(new Triple(node, RDFType, ClsAdverb));
                    break;
                case WordSense.ADJECTIVE:
                    graph.Assert(new Triple(node, RDFType, ClsAdjective));
                    break;
                case WordSense.PREPOSITION:
                    graph.Assert(new Triple(node, RDFType, ClsPreposition));
                    break;
                default:
                    graph.Assert(new Triple(node, RDFType, ClsNoun));
                    break;
            }
            graph.Assert(new Triple(node, RDFSLabel, label));
            if (true)
            {
                label = graph.CreateLiteralNode(objWordSense.getPicture());
                graph.Retract(graph.GetTriplesWithSubjectPredicate(node, HasPicture));
                Triple triple = new Triple(node, HasPicture, label);
                graph.Assert(triple);
            }			
		}
				
		public List<Word> getWordList() {
			
			Object res = graph.ExecuteQuery(
				"SELECT distinct ?label " +
				"WHERE { " +
				"?id <" + rdf + "type> <" + word + "Word>. " +
				"?id <" + rdfs + "label> ?label. " +
				"}");

			List<Word> lst = null;			
			Word w;
			if (res is SparqlResultSet) {
				SparqlResultSet rs = (SparqlResultSet) res;
                lst = new List<Word>();
				foreach (SparqlResult r in rs) {
					w = new Word(r.Value("label").ToString());
                    lst.Add(w);
				}
			}
            return lst;
		}
		
		public ArrayList getWordSenseList() {

			Object res = graph.ExecuteQuery(
				"SELECT distinct ?word ?pic " +
				"WHERE { " +
				"?id <" + rdf + "type> <" + word + "WordSense>. " +
				"?id <" + rdfs + "label> ?word. " +
				"OPTIONAL { " +
				"?id <" + word + "hasPicture> ?pic. " +
				"}" +
				"}");

			ArrayList ret = null;			
			WordSense ws;
			if (res is SparqlResultSet) {
				SparqlResultSet rs = (SparqlResultSet) res;
				ret = new ArrayList();
				foreach (SparqlResult r in rs) {
					if (r.HasValue("pic"))
						ws = new WordSense(0,r.Value("word").ToString(), r.Value("pic").ToString());
					else
						ws = new WordSense();
					ret.Add(word);
				}
			}
			return ret;
		}
			
		public List<Word> getIsARootWordNodes() {
			Object res = graph.ExecuteQuery(
                "SELECT distinct ?word " +
                "WHERE { " +
                "?id <" + rdfs + "label> ?word. " +
                "?id <" + word + "hasSense> ?sid. " +
                "FILTER (NOT EXISTS {?sid <" + word + "isA> ?s }) " +
                "}");

			List<Word> reList = null;			
			Word w;
			if (res is SparqlResultSet) {
				SparqlResultSet rs = (SparqlResultSet) res;
                reList = new List<Word>();
				foreach (SparqlResult r in rs) {                    
					w = new Word(r.Value("word").ToString());
                    reList.Add(w);
				}
			}
            return reList;			
		}

        public List<SimpleWord> getIsARootSimpleWordNodes()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?word ?label ?type ?pic " +
                "WHERE { " +
                "?id <" + rdfs + "label> ?word. " +
                "?id <" + word + "hasSense> ?sid. " +
                "?sid <" + rdf + "type> ?type. " +
                "?sid <" + rdfs + "label> ?label. " +
                "?sid <" + word + "hasPicture> ?pic. " +
                "FILTER (NOT EXISTS {?sid <" + word + "isA> ?s }) " +
                "}");

            List<SimpleWord> reList = null;
            SimpleWord sw = null;
            WordSense sense = null;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                reList = new List<SimpleWord>();
                foreach (SparqlResult r in rs)
                {
                    sense = new WordSense(getWordSenseTypeFromString(r.Value("type").ToString()), r.Value("label").ToString(), r.Value("pic").ToString());
                    sw = new SimpleWord(r.Value("word").ToString(), sense);
                    reList.Add(sw);
                }
            }
            return reList;	
        }

        public List<WordSense> getIsARootWordSenseNodes()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?type ?label ?pic " +
                "WHERE { " +
                "?id <" + rdf + "type>" + " ?type. " +
                "?type <" + rdfs + "subClassOf> <" + word + "WordSense>. " +
                "?id <" + rdfs + "label> ?label. " +
                "OPTIONAL { " +
                "?id <" + word + "hasPicture> ?pic. " +
                "} " +
                "FILTER (NOT EXISTS {?id <" + word + "isA> ?v}) " +
                "}");
            List<WordSense> reList = null;
            WordSense ws;
            String strType;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                reList = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    strType = r.Value("type").ToString();
                    ws = new WordSense(getWordSenseTypeFromString(strType), r.Value("label").ToString(), r.Value("pic").ToString());
                    reList.Add(ws);
                }
            }
            return reList;
        }

        public List<WordSense> getIsARootWordSenseNodes(String type)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label ?pic " +
                "WHERE { " +
                "?id <" + rdf + "type> <" + word + type + ">. " +
                //"?type <" + rdfs + "subClassOf> <" + word + "WordSense>. " +
                "?id <" + rdfs + "label> ?label. " +
                "OPTIONAL { " +
                "?id <" + word + "hasPicture> ?pic. " +
                "} " +
                "FILTER (NOT EXISTS {?id <" + word + "isA> ?v}) " +
                "}");
            List<WordSense> reList = null;
            WordSense ws;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                reList = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    ws = new WordSense(getWordSenseTypeFromString(type), r.Value("label").ToString(), r.Value("pic").ToString());
                    reList.Add(ws);
                }
            }
            return reList;
        }
		
		public int getWordSenseTypeFromString(String strType) {
			if (strType.IndexOf("Noun") > -1)
				return WordSense.NOUN;
            else if (strType.IndexOf("Adjective") > -1)
				return WordSense.ADJECTIVE;
            else if (strType.IndexOf("Adverb") > -1)
				return WordSense.ADVERB;
            else if (strType.IndexOf("Verb") > -1)
				return WordSense.VERB;
			else
				return WordSense.PREPOSITION;
		}

        public List<WordSense> getAllWordSenseOfWord(Word objWord)
        {
            //MessageBox.Show(objWord.getID());
            Object res = graph.ExecuteQuery(
                "SELECT ?type ?label ?pic " +
                "WHERE { " +
                "<" + word + objWord.getID() + "> <" + word + "hasSense> ?id. " +
                "?id <" + rdfs + "label> ?label. " +
                "?id <" + rdf + "type> ?type. " +
                "OPTIONAL { " +
                "?id <" + word + "hasPicture> ?pic. " +
                "} " +
                "}");
            List<WordSense> list = null;
            WordSense ws;
            String strType;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                list = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    strType = r.Value("type").ToString();
                    ws = new WordSense(getWordSenseTypeFromString(strType), r.Value("label").ToString(), r.Value("pic").ToString());
                    list.Add(ws);
                }
            }
            return list;
        }
		
        public List<Word> getIsAChildWordNodes(IResource objWord)
        {
            Object res = graph.ExecuteQuery(
                "SELECT ?word " +
                "WHERE { <" + word + objWord.getID() + "> <" + word + "hasSense> ?sid1. " +
                "?sid <" + word + "isA> ?sid1. " +
                "?id <" + word + "hasSense> ?sid." +
                "?id <" + rdfs + "label> ?word. " +
                "}"
                );

			List<Word> reList = null;			
			Word w;

			if (res is SparqlResultSet) {
				SparqlResultSet rs = (SparqlResultSet) res;
                reList = new List<Word>();
				foreach (SparqlResult r in rs) {	
                    w = new Word(r.Value("word").ToString());
                    reList.Add(w);
				}
			}

            return reList;						
		}

        public List<SimpleWord> getIsAChildSimpleWordNodes(IResource objWord)
        {
            Object res = graph.ExecuteQuery(
                "SELECT ?word ?label ?type ?pic " +
                "WHERE { <" + word + objWord.getID() + "> <" + word + "hasSense> ?sid1. " +
                "?sid <" + word + "isA> ?sid1. " +
                "?id <" + word + "hasSense> ?sid." +
                "?id <" + rdfs + "label> ?word. " +
                "?sid <" + rdf + "type> ?type. " +
                "?sid <" + rdfs + "label> ?label. " +
                "?sid <" + word + "hasPicture> ?pic. " +
                "}"
                );

            List<SimpleWord> reList = null;
            SimpleWord sw = null;
            WordSense sense = null;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                reList = new List<SimpleWord>();
                foreach (SparqlResult r in rs)
                {
                    sense = new WordSense(getWordSenseTypeFromString(r.Value("type").ToString()), r.Value("label").ToString(), r.Value("pic").ToString());
                    sw = new SimpleWord(r.Value("word").ToString(), sense);
                    reList.Add(sw);
                }
            }

            return reList;
        }

        public List<WordSense> getIsAChildWordSenseNodes(IResource objWordSense)
        {
            Object res = graph.ExecuteQuery(
                "SELECT ?label ?type ?pic " +
                "WHERE { " +
                "?id <" + word + "isA> <" + word + objWordSense.getID() + ">. " +
                "?id <" + rdfs + "label> ?label. " +
                "?id <" + rdf + "type> ?type. " +
                "OPTIONAL { " +
                "?id <" + word + "hasPicture> ?pic. " +
                "} " +
                "}");

            List<WordSense> reList = null;
            WordSense ws;
            String strType;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                reList = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    strType = r.Value("type").ToString();
                    ws = new WordSense(getWordSenseTypeFromString(strType), r.Value("label").ToString(), r.Value("pic").ToString());
                    reList.Add(ws);
                }
            }

            return reList;
        }

        //get word relation IsA
        public Word getWordIsAWord(WordSense nSense)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + nSense.getID() + "> <" + word + "isA> ?sid. " +
                "?id <" + word + "hasSense> ?sid. " +
                "?id <" + rdfs + "label> ?label. " +
                "}");
            Word reW = null;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                foreach (SparqlResult r in rs)
                {
                    reW = new Word(r.Value("label").ToString());
                }
            }

            return reW;
        }
		
		public WordSense getWordSense(IResource objWord) {
			WordSense ret = null;
			
			INode node = graph.CreateUriNode("word:" + objWord.getID());
			IEnumerator t = graph.GetTriplesWithSubjectPredicate(node, HasSense).GetEnumerator();
			if (t.MoveNext()) {
				node = ((Triple) t.Current).Object;
				t = graph.GetTriplesWithSubjectPredicate(node, RDFSLabel).GetEnumerator();
				if (t.MoveNext()) {
					ret = new WordSense();
					ILiteralNode label = (ILiteralNode)((Triple) t.Current).Object;
					//ret.setLabel(label.Value);
					t = graph.GetTriplesWithSubjectPredicate(node, HasPicture).GetEnumerator();
					if (t.MoveNext()) {					
						label = (ILiteralNode)((Triple) t.Current).Object;
						ret.setPicture(label.Value);
					}
				}
			}						
			return ret;
		}

        public List<Word> getWordSynonym(Word nW, WordSense nWS)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + nWS.getID() + "> <" + word + "synonymWith> ?sid. " +
                "?id <" + word + "hasSense> ?sid. " +
                "?id <" + rdfs + "label> ?label. " +
                "FILTER ( ?id != <" + word + nW.getID() + "> ) " +
                "}");

            List<Word> retn = null;
            Word w;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<Word>();
                foreach (SparqlResult r in rs)
                {
                    w = new Word(r.Value("label").ToString());
                    retn.Add(w);
                }
            }
            return retn;
        }

        public List<Word> getWordSynonym(WordSense nWS)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + nWS.getID() + "> <" + word + "synonymWith> ?sid. " +
                "?id <" + word + "hasSense> ?sid. " +
                "?id <" + rdfs + "label> ?label. " +
                "}");

            List<Word> retn = null;
            Word w;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<Word>();
                foreach (SparqlResult r in rs)
                {
                    w = new Word(r.Value("label").ToString());
                    retn.Add(w);
                }
            }
            return retn;
        }

        public List<Word> getWordAntonym(WordSense nWS)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + nWS.getID() + "> <" + word + "antonymWith> ?sid. " +
                "?id <" + word + "hasSense> ?sid. " +
                "?id <" + rdfs + "label> ?label. " +
                "}");

            List<Word> retn = null;
            Word w;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<Word>();
                foreach (SparqlResult r in rs)
                {
                    w = new Word(r.Value("label").ToString());
                    retn.Add(w);
                }
            }
            return retn;
        }

        public List<WordSense> getWordSenseOfWord(IResource objWord)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id ?type ?label ?pic " +
                "WHERE { " +
                "<" + word + objWord.getID() + "> <" + word + "hasSense> ?id. " +
                "?id <" + rdfs + "label> ?label. " +
                "?id <" + word + "hasPicture> ?pic. " +
                "?id <" + rdf + "type> ?type. " +
                "}");

            List<WordSense> retn = null;
            WordSense sw;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    //String label = r.Value("label").ToString();
                    sw = new WordSense(getWordSenseTypeFromString(r.Value("type").ToString()), r.Value("label").ToString(), r.Value("pic").ToString());
                    retn.Add(sw);
                }
            }
            return retn;
        }
		
		public Word getAntonymWith(Word objWord) {
			Word ret = null;
			
			INode node = graph.CreateUriNode("word:" + objWord.getID());
			IEnumerator t = graph.GetTriplesWithSubjectPredicate(node, AntonymWith).GetEnumerator();
			if (t.MoveNext()) {
				ret = new Word();
				node = ((Triple) t.Current).Object;
				ILiteralNode label;
				
				t = graph.GetTriplesWithSubjectPredicate(node, RDFSLabel).GetEnumerator();
				if (t.MoveNext()) {
					label = (ILiteralNode)((Triple) t.Current).Object;
					//ret.setLabel(label.Value);
				}
					
				t = graph.GetTriplesWithSubjectPredicate(node, RDFType).GetEnumerator();
				if (t.MoveNext()) {					
					INode nodeTmp = ((Triple) t.Current).Object;
					t = graph.GetTriplesWithSubjectPredicate(nodeTmp, RDFSLabel).GetEnumerator();
					if (t.MoveNext()) {
						label = (ILiteralNode)((Triple) t.Current).Object;
						//ret.setType(getWordSenseTypeFromString(label.Value));						
					}					
				}
			}						
			return ret;			
		}

        public List<Question> getQuestion()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id ?type ?date " +
                "WHERE { " +
                "?id <" + rdf + "type> <" + word + "Question>. " +
                "?id <" + word + "qdateCreate> ?date. " +
                "?id <" + word + "hasType> ?type. " +
                "}");
            List<Question> lst = null;
            Question qt = null;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                lst = new List<Question>();
                foreach (SparqlResult r in rs)
                {
                    qt = new Question(r.Value("id").ToString().Remove(0, word.ToString().Length), int.Parse(r.Value("type").ToString()), r.Value("date").ToString());
                    lst.Add(qt);
                }
            }
            return lst;
        }

        public List<WordSense> getWordSenseInQuestion(Question qt)
        {
            //System.Windows.Forms.MessageBox.Show(qt.getQuestionID());
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id ?type ?label ?pic " +
                "WHERE { " +
                "<" + word + qt.getID() + "> <" + word + "joinWordSense> ?id. " +
                "?id <" + rdfs + "label> ?label. " +
                "?id <" + word + "hasPicture> ?pic. " +
                "?id <" + rdf + "type> ?type. " +
                "}");

            List<WordSense> retn = null;
            WordSense sw;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<WordSense>();
                foreach (SparqlResult r in rs)
                {
                    //String label = r.Value("label").ToString();
                    sw = new WordSense(getWordSenseTypeFromString(r.Value("type").ToString()), r.Value("label").ToString(), r.Value("pic").ToString());
                    retn.Add(sw);
                }
            }
            return retn;
        }

        public List<Word> getWordInQuestion(Question qt)
        {
            //System.Windows.Forms.MessageBox.Show(qt.getQuestionID());
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + qt.getID() + "> <" + word + "joinWord> ?id. " +
                "?id <" + rdfs + "label> ?label. " +
                "}");

            List<Word> retn = null;
            Word w;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<Word>();
                foreach (SparqlResult r in rs)
                {
                    w = new Word(r.Value("label").ToString());
                    retn.Add(w);
                }
            }
            return retn;
        }

        public List<SimpleWord> getSimpleWordInQuestion(Question qt)
        {
            //System.Windows.Forms.MessageBox.Show(qt.getQuestionID());
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + qt.getID() + "> <" + word + "joinWord> ?id. " +
                "?id <" + rdfs + "label> ?label. " +
                "}");

            List<SimpleWord> retn = null;
            SimpleWord w;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<SimpleWord>();
                foreach (SparqlResult r in rs)
                {
                    w = new SimpleWord(r.Value("label").ToString(), null);
                    retn.Add(w);
                }
            }
            return retn;
        }

        public List<Question> getQuestionInTest(Test test)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?q ?type ?date " +
                "WHERE { " +
                "<" + word + test.getID() + "> <" + word + "joinQuestion> ?q. " +
                "?q <" + word + "hasType> ?type. " +
                "?q <" + word + "qdateCreate> ?date. " +
                "}");
            List<Question> retn = null;
            Question q;

            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                retn = new List<Question>();
                foreach (SparqlResult r in rs)
                {
                    q = new Question(r.Value("q").ToString().Remove(0, word.ToString().Length), int.Parse(r.Value("type").ToString()), r.Value("date").ToString());
                    retn.Add(q);
                }
            }
            return retn;
        }

        public int CountQuestionInTest(Test test)
        {
            return getQuestionInTest(test).Count();
        }

        public int getMaxIndexQuestion()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id " +
                "WHERE { " +
                "?id <" + rdf + "type> <" + word + "Question>. " +
                "}");
            int retn = 0;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                foreach (SparqlResult r in rs)
                {
                    int i = int.Parse(r.Value("id").ToString().Substring(r.Value("id").ToString().LastIndexOf("_") + 1));
                    if (i > retn) retn = i;
                }
            }
            return retn;
        }

        public List<Test> getTest()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id ?date ?time ?name " +
                "WHERE { " +
                "?id <" + rdf + "type> <" + word + "Test>. " +
                "?id <" + word + "tdateCreate> ?date. " +
                "?id <" + word + "time> ?time. " +
                "?id <" + rdfs + "label> ?name. " +
                "}");
            List<Test> lst = null;
            Test tst = null;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                lst = new List<Test>();
                foreach (SparqlResult r in rs)
                {
                    tst = new Test( r.Value("id").ToString().Remove(0, word.ToString().Length), 
                                    r.Value("name").ToString(), 
                                    r.Value("date").ToString(), 
                                    int.Parse(r.Value("time").ToString())) ;
                    int j = CountQuestionInTest(tst);
                    tst.setNumberQuestion(j);
                    lst.Add(tst);
                }
            }
            return lst;
        }

        public int getMaxIndexTest()
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?id " +
                "WHERE { " +
                "?id <" + rdf + "type> <" + word + "Test>. " +
                "}");
            int retn = 0;
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                foreach (SparqlResult r in rs)
                {
                    int i = int.Parse(r.Value("id").ToString().Substring(r.Value("id").ToString().LastIndexOf("_") + 1));
                    if (i > retn) retn = i;
                }
            }
            return retn;
        }

        public bool IsExist(String id)
        {
            Object res = graph.ExecuteQuery(
                "SELECT distinct ?label " +
                "WHERE { " +
                "<" + word + id + "> <" + rdfs + "label> ?label. " +
                "}");
            if (res is SparqlResultSet)
            {
                SparqlResultSet rs = (SparqlResultSet)res;
                if (rs.Count > 0) return true;
            }
            return false;
        }
			
		public void save(String file) {
			RdfXmlWriter w = new RdfXmlWriter();
			w.Save(graph, file);
		}
		
		public void save() {
			if (strFileName != null) {
				RdfXmlWriter w = new RdfXmlWriter();
				w.Save(graph, strFileName);
			}
				
		}
    }
}
